#ifndef QLANGUAGEMANAGER_H
#define QLANGUAGEMANAGER_H

#include "sharedlibglobal.h"

#include <QMap>
#include <QObject>
#include <QList>
#include <QStringList>

class QXmlStreamReader;

namespace SHAREDLIB
{
    class SHAREDLIB_EXPORT tagLanguageItem
    {
    public:
        QString                 m_id;
        QMap<QObject*,QString>  m_translate;

        bool operator == (const tagLanguageItem &item)
        {
            return m_id==item.m_id;
        }

        tagLanguageItem & operator =(const tagLanguageItem &item)
        {
            m_id=item.m_id;
            m_translate=item.m_translate;
            return *this;
        }
    };

    class SHAREDLIB_EXPORT QLanguageManager : public QObject
    {
        Q_OBJECT
    public:
        explicit QLanguageManager(QObject *parent = 0);
        ~QLanguageManager();

        void        addLanguage(QObject* obj);
        void        insertLanguage(int index,QObject* obj);
        void        removeLanguage(const QString& languageName);
        QObject*    getLanguage(int index);
        QObject*    getLanguage(const QString &languageName);
        int         languageIndex(QObject* obj);
        int         languageCount();
        QStringList languages();

        void        setLanguageName(QObject* obj,const QString& name);

        void        swapLanguage(int index1,int index2);

        void        setCurrentLanguage(const QString &languageName);
        void        setCurrentLanguage(QObject* language);
        QObject*    getCurrentLanguage();

        QString     getText(const QString &languageName,const QString &id);
        QString     getCurrentText(const QString &id);

        void        save(const QString &projectPath);
        void        load(const QString &projectPath);
        void        release();

        tagLanguageItem *getTextItem(const QString &id);
        void        addTextItem(tagLanguageItem *item);
        void        removeTextItem(const QString &id);
        int         getItemCount();
        void        setText(const QString &language,const QString &id,const QString &text);
        tagLanguageItem *getTextItem(int index);

        void        refreshLanguage();
    protected:
        void        loadLanguage(QXmlStreamReader *r);

        void        saveTranslate(const QString &projectPath);
        void        loadTranslate(const QString &projectPath);
        void        loadTranslageItem(QXmlStreamReader *r);
    signals:
        void        currentLanguageChanged(QObject* current);
        void        refresh();
        void        languageAdd(QObject* obj,int index);
        void        languageDel(QObject* obj);
        void        refreshUi(const QString &id);
        void        itemChanged(SHAREDLIB::tagLanguageItem *item);
        void        removeItem(const QString &id);
        void        languageNameChanged(const QString &old,const QString &now);
    public slots:
    protected:
        QList<QObject*>     m_languages;
        QObject*            m_current;

        QList<tagLanguageItem*> m_texts;
    };

}
#endif // QLANGUAGEMANAGER_H
